
package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.BIN;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.BN;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.ED;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.LIST_BN;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TEL;

import java.util.Iterator;


//            ANYImpl, BINImpl, BLImpl, CSImpl,
//            EDImpl, INTImpl, NullFlavorImpl, STImpl, 
//            TELImpl

public abstract class STCommonImpl extends ANYImpl
    implements ST
{

    public static final String RCS_ID = "$Header: STCommonImpl.java 1.0 2006/07/15 11:47:10 anbindal noship $";
    protected String _value;
    protected CS _language;
    protected static final String MEDIA_TYPE_STR = "text/plain";
    protected static final String CHAR_SET_STR = "UTF-16";

    protected STCommonImpl(String s)
    {
        this(s, CSImpl.nullCS(NullFlavorImpl.NI));
    }

    protected STCommonImpl(String s, CS cs)
    {
        if(s == null)
        {
            throw ANYImpl.newNullException("ST", "constructor", "value");
        } else
        {
            _value = s;
            _language = cs;
            return;
        }
    }

    protected STCommonImpl(CS cs)
    {
        super(true, cs);
        _value = null;
    }

    public INT length()
    {
        if(isNull().isTrue())
            return INTImpl.valueOf(0L);
        else
            return INTImpl.valueOf(_value.length());
    }

    public ST headCharacter()
    {
        if(isNull().isTrue())
            return new STImpl(NullFlavorImpl.NI);
        else
            return STImpl.valueOf(_value.charAt(0));
    }

    public ST tailString()
    {
        if(isNull().isTrue())
            return new STImpl(NullFlavorImpl.NI);
        else
            return STImpl.valueOf(_value.substring(1));
    }

    public CS charset()
    {
        if(isNull().isTrue())
            return CSImpl.nullCS(NullFlavorImpl.NI);
        else
            return CSImpl.valueOf("UTF-16");
    }

    public CS compression()
    {
        return new CSImpl(NullFlavorImpl.NA);
    }

    public BIN integrityCheck()
    {
        return new BINImpl(NullFlavorImpl.NA);
    }

    public CS integrityCheckAlgorithm()
    {
        return new CSImpl(NullFlavorImpl.NA);
    }

    public CS language()
    {
        if(isNull().isTrue())
            return CSImpl.nullCS(NullFlavorImpl.NI);
        else
            return _language;
    }

    public CS mediaType()
    {
        if(isNull().isTrue())
            return CSImpl.nullCS(NullFlavorImpl.NI);
        else
            return CSImpl.valueOf("text/plain");
    }

    public TEL reference()
    {
        return new TELImpl(NullFlavorImpl.NI);
    }

    public ED thumbnail()
    {
        return new EDImpl(NullFlavorImpl.NI);
    }

    public CS representation()
    {
        return CSImpl.valueOf("TXT");
    }

    public ST literal()
    {
        return this;
    }

    public BL contains(BN bn)
    {
        throw ANYImpl.newUnsupportedException("ST", "contains");
    }

    public BN head()
    {
        throw ANYImpl.newUnsupportedException("ST", "head");
    }

    public BL isEmpty()
    {
        if(isNull().isTrue())
            return BLImpl.TRUE;
        else
            return BLImpl.FALSE;
    }

    public BN item(INT int1)
    {
        throw ANYImpl.newUnsupportedException("ST", "item");
    }

    public BL notEmpty()
    {
        if(isNull().isTrue())
            return BLImpl.FALSE;
        else
            return BLImpl.TRUE;
    }

    public LIST_BN tail()
    {
        throw ANYImpl.newUnsupportedException("ST", "tail");
    }

    public Iterator listIterator()
    {
        throw ANYImpl.newUnsupportedException("ST", "listIterator");
    }

    public String toString()
    {
        if(isNull().isTrue())
            return "<Null:" + nullFlavor().toString() + ">";
        else
            return _value;
    }

    public String stringValue()
    {
        return _value;
    }

    public int hashCode()
    {
        int i = 17;
        if(isNull().isFalse())
            i = 37 * i + _value.hashCode();
        return i;
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = (XMLElement)xmldocument.createElement(s);
//            String s1 = stringValue();
//            if(s1 != null)
//            {
//                org.w3c.dom.Text text = xmldocument.createTextNode(s1);
//                xmlelement.appendChild(text);
//            }
//            xmlelement.setAttribute("encoding", "TXT");
//            xmlelement.setAttribute("mediaType", "text/plain");
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }
}
